package cn.schoolwow.workflow.test.module.oa.common.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDefinitionOption;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployFileContext;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class DeployConvertWorkFlowFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String deployPath = flowContext.checkData("deployPath", String.class);
        String deployName = flowContext.checkData("deployName", String.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        JSONObject workflow = QueryResource.newQuery("部署流程原始数据", deployPath)
                .execute()
                .getAsJSONObject();
        flowContext.startFlow(new ConvertToWorkFlowDeployFileContextFlow())
                .putCurrentCompositeFlowData("workflow", workflow)
                .execute();
        WorkFlowDeployFileContext workFlowDeployFileContext = flowContext.checkInstanceData(WorkFlowDeployFileContext.class);
        System.out.println("部署工作流,名称["+deployName+"],工作流JSON数据:"+JSON.toJSONString(workFlowDeployFileContext));

        WorkFlowDeployRequest workFlowDeployRequest = new WorkFlowDeployRequest();
        workFlowDeployRequest.name = deployName;
        workFlowDeployRequest.workFlowDeployFileContext = workFlowDeployFileContext;
        {
            WorkFlowDefinitionOption workFlowDefinitionOption = new WorkFlowDefinitionOption();
            workFlowDefinitionOption.intelligent = false;
            workFlowDefinitionOption.allowAdd = true;
            workFlowDefinitionOption.allowRevoke = true;
            workFlowDeployRequest.workFlowDefinitionOption = workFlowDefinitionOption;
        }
        quickWorkFlow.deployWorkFlow(workFlowDeployRequest);
    }

    @Override
    public String name() {
        return "转换原始数据为WorkFlowDeployRequest对象";
    }

}
